package util

import (
	"fmt"
	"math"
	"reflect"
)

func maxValueInMap(m map[string]int) int {
	max := -1
	for _, v := range m {
		if v > max {
			max = v
		}
	}
	return max
}

// 根据项目时长以及人员角色计算审前审后时间
func AuditDay(proposedDay int, role string, rule map[string]map[string]map[string]int) (int, int) {
	week := int(math.Ceil(float64(proposedDay) / 7))
	weekString := fmt.Sprintf("%d", week)
	roleDict := rule[role]
	beforeDict := roleDict["审前"]
	afterDict := roleDict["审后"]
	before, ok := beforeDict[weekString]
	if !ok {
		before = maxValueInMap(beforeDict)
	}
	after, ok := afterDict[weekString]
	if !ok {
		after = maxValueInMap(afterDict)
	}
	return before, after
}

// 两区间是否有交叉
func CrossOver(regionALeft, regionARight, regionBLeft, regionBRight string) bool {
	if regionBLeft <= regionALeft && regionALeft <= regionBRight {
		return true
	}
	if regionBLeft <= regionARight && regionARight <= regionBRight {
		return true
	}
	if regionBLeft >= regionALeft && regionARight >= regionBRight {
		return true
	}
	return false
}

func CrossOverMulti(regionALeft, regionARight string, region []map[string]string) bool {
	for _, r := range region {
		if CrossOver(regionALeft, regionARight, r["startDate"], r["endDate"]) {
			return true
		}
	}
	return false
}

func MapValueNotEmpty(m *map[string]string) bool {
	for _, value := range *m {
		if value == "" {
			return false
		}
	}
	return true
}

func Duplicate(a interface{}) (ret []interface{}) {
	va := reflect.ValueOf(a)
	for i := 0; i < va.Len(); i++ {
		if i > 0 && reflect.DeepEqual(va.Index(i-1).Interface(), va.Index(i).Interface()) {
			continue
		}
		ret = append(ret, va.Index(i).Interface())
	}
	return ret
}

func Scale(proposedDay int, ProjectScale map[string]string) string {
	week := int(math.Ceil(float64(proposedDay) / 7))
	weekString := fmt.Sprintf("%d", week)
	s, ok := ProjectScale[weekString]
	if ok {
		return s
	}
	return "bigProject"
}
